<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>绘制文本</title>
</head>
<body>
  <canvas id="drawing" width="200" height="200"></canvas>
  <script>
    const drawing = document.getElementById('drawing');

    // 确保浏览器支持 <canvas>
    if (drawing.getContext) {
      const context = drawing.getContext('2d');

      let fontSize = 100;
      context.font = fontSize + 'px Arial';
      while (context.measureText('Hello world!').width > 140) {
        fontSize--;
        context.font = fontSize + 'px Arial';
      }
      context.fillText('Hello world!', 10, 30);
      context.fillText('Font size is' + fontSize + 'px', 10, 70);
    }
  </script>
</body>
</html>